package com.threeox.commonlibrary.ui.fragment.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView.LayoutStyle;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView.TopBarStyle;
import com.threeox.commonlibrary.ui.view.inter.titlebar.OnTopBarListener;
import com.threeox.commonlibrary.util.ViewUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.ToastUtils;

/** 
 *
 * @ClassName: CommonFragment
 *
 * @Description: (Fragment 基库)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/19 17:41 
 * 
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public abstract class CommonFragment extends Fragment implements OnClickListener, OnTopBarListener {

    protected View mView;
    protected Intent mIntent;
    protected Bundle mBundle;
	protected Activity mContext;
    protected View mContentView;
    protected ViewUtils mViewUtils;
	protected LayoutInflater mInflater;
	protected CommonApplcation mApplication;
    protected CommonTitleBarView mTitleBarView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setRetainInstance(true);
			this.mContext = getActivity();
            this.mBundle = getArguments();
            this.mIntent = mContext.getIntent();
            this.mViewUtils = ViewUtils.newInstance();
            this.mApplication = CommonApplcation.getInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public <T extends View> T findViewById(int id) {
		try {
			return (T) mView.findViewById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 初始化只有标题
	 * 
	 * @param tbs
	 * @param title
	 */
	protected void initTitle(TopBarStyle tbs, CharSequence title) {
        mTitleBarView = findViewById(R.id.id_topbar_view);
		if (mTitleBarView == null) {
            mTitleBarView = new CommonTitleBarView(mContext);
		}
        mTitleBarView.init(tbs, title);
        mTitleBarView.setOnTopbarListener(this);
	}

	/**
	 * 初始化更改布局的标题栏
	 * 
	 * @param tbs
	 * @param ls
	 * @param title
	 */
	protected <T> T initTitle(TopBarStyle tbs, LayoutStyle ls, int layoutId, CharSequence title) {
		initTitle(tbs, title);
		View v = mTitleBarView.setLayout(layoutId, ls);
		if (layoutId == R.layout.title_bar_text)
			setTextVal((TextView) v);
		return (T) v;
	}

	/**
	 * 初始化更改布局的标题栏
	 * 
	 * @param tbs
	 * @param ls
	 * @param title
	 */
	protected <T> T initTitle(TopBarStyle tbs, LayoutStyle ls1, LayoutStyle ls, int layoutId, CharSequence title) {
		initTitle(tbs, title);
		View v = mTitleBarView.setLayout(layoutId, ls);
		if (layoutId == R.layout.title_bar_text)
			setTextVal((TextView) v);
		return (T) v;
	}

	protected void setTextVal(TextView v) {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.mInflater = inflater;
		mView = setView(container);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initViews(savedInstanceState);
		initDatas();
        setListener();
	}

	protected abstract View setView(ViewGroup container);

	protected abstract void initViews(Bundle savedInstanceState);

	protected abstract void initDatas();

	protected abstract void setListener();

	protected View initLayout(int layoutId, int fatherId) {
		return mInflater.inflate(layoutId, (ViewGroup) getActivity().findViewById(fatherId), false);
	}

	protected View initLayout(int layoutId, ViewGroup container) {
		return mInflater.inflate(layoutId, null, false);
	}

	public void runOnWorkThread(Runnable action) {
		new Thread(action).start();
	}

	protected void showToast(String text) {
		ToastUtils.showLongToastSafe(getActivity(), text);
	}

	/**
	 * 打Log ShowLog
	 * 
	 * @return void
	 * @throws
	 */
	public void ShowLog(String msg) {
		LogUtils.e(msg);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 设置Value
	 * 
	 * @param viewId
	 * @param val
	 */
	protected void setValue(int viewId, Object val) {
		View view = findViewById(viewId);
		//ViewUtils.setValue(view, val);
        mViewUtils.setValue(view, val);
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public boolean onLeftLayoutClick(View v) {
		return true;
	}

	@Override
	public void onRightLayoutClick(View v) {

	}

	@Override
	public void onCenterLayoutClick(View v) {

	}

}
